package com.buybuyyi.stylistcrm.object.util;

import java.util.concurrent.ConcurrentHashMap;

import org.springframework.cglib.beans.BeanCopier;


public class BeanTranslater {

	private static ConcurrentHashMap<String, BeanCopier> beanCopierMap = new ConcurrentHashMap<String, BeanCopier>();
	private static final Object lock = new Object();
	
	public static void copyProperties(Object source, Object target) {
		String beanKey = generateKey(source.getClass(), target.getClass());
		BeanCopier copier = beanCopierMap.get(beanKey);
		if (copier == null) {
			synchronized (lock) {
				copier = beanCopierMap.get(beanKey);
				if (copier == null) {
					copier = BeanCopier.create(source.getClass(), target.getClass(), false);
					beanCopierMap.putIfAbsent(beanKey, copier);// putIfAbsent已经实现原子操作了。
				}
			}

		}
		copier.copy(source, target, null);
	}
	
	private static String generateKey(Class<?> class1, Class<?> class2) {
		return class1.toString() + class2.toString();
	}
}
